package com.programming.forum.controller;

import com.programming.forum.common.AppResult;
import com.programming.forum.common.ResultCode;
import com.programming.forum.dao.BoardMapper;
import com.programming.forum.exception.ApplicationException;
import com.programming.forum.model.Board;
import com.programming.forum.services.impl.BoardServiceImpl;
import com.sun.istack.internal.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22479
 * Date: 2023-11-07
 * Time: 16:59
 */
@RestController
@Api(tags = "版块接口")
@Slf4j
@RequestMapping("/board")
public class BoardController {

    @Value("${forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Resource
    private BoardServiceImpl boardService;
    /**
     * 查询首页版块列表
     * @return
     */
    @ApiOperation("获取首页版块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList() {
        log.info("首页版块个数" + indexBoardNum);
        //调用service查询结果
        List<Board> boardList = boardService.selectByNum(indexBoardNum);
        if(boardList == null) {
            boardList = new ArrayList<>();
        }
        return AppResult.success(boardList);
    }

    @GetMapping("/geyById")
    @ApiOperation("获取版块信息")
    public AppResult<Board> getById(@ApiParam("版块Id") @RequestParam("id") @NotNull Long id) {
      //调用service
        Board board = boardService.selectById(id);

        //校验结果 版块不存在 或 版块删除State == 1
        if(board == null || board.getDeleteState() == 1) {
            //FAILED_BOARD_BANNED      (1203,"版块状况异常, 请联系管理员."),
            log.info(ResultCode.FAILED_BOARD_BANNED  .toString());

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_BANNED  ));
        }
        return AppResult.success(board);
    }
}
